<?php
/**
 * 央票通商城
 * @author lzp
 * @date 2016-10-9
 */

class ShopService extends BaseService
{
    private static $self = NULL;
    private $mShopProduct      =  NULL;
    private $mShopBusiness     =  NULL;
    private $mCategory         =   NULL;
    private $mSpecificate      =   NULL;
    private $mShopBrand        =   NULL;

    public function __construct()
    {
        $this->mShopProduct   =   new Involved_ShopProductModel();
        $this->mShopBusiness  =   new Involved_ShopBusinessModel();
        $this->mCategory      =   new Involved_ShopModel();
        $this->mSpecificate   =   new  Involved_SpecifiModel();
        $this->mShopBrand     =   new  Involved_ShopBrandModel();
    }
    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }


    /**
     * 商品列表
     * @return mixed
     */
    public function ProductList()
    {
        return $this->mShopProduct->ProductList();
    }

    /**
     * 商品添加
     * @param $paramObj
     * @return int
     */
    public function ProductAdd($paramObj)
    {

        $paramObj->img1 = !empty($paramObj->img1) ?
            $paramObj->img1      =    QiniuUpload::instance()->upload_files($paramObj->img1):'';
        $paramObj->img2 = !empty($paramObj->img2) ?
            $paramObj->img2      =    QiniuUpload::instance()->upload_files($paramObj->img2):'';
        $paramObj->img3 = !empty($paramObj->img3) ?
            $paramObj->img3      =    QiniuUpload::instance()->upload_files($paramObj->img3):'';

        $aData = [
                  'product_id'          =>             $paramObj->id,                              //产品id
                  'product_name'        =>             $paramObj->product_name,                    //产品名称
                  'price'               =>             $paramObj->price,                           //价格
                  'brand'               =>             $paramObj->brand,                           //品牌
                  'spec'                =>             $paramObj->spec,                            //规格
                  'sort'                =>             $paramObj->sort,                            //分类
                  'stock'               =>             $paramObj->stock,                           //库存
                  'place'               =>             $paramObj->place,                           //产地
                  'business_id'         =>             $paramObj->business_id,                     //商家id
                  'img1'                =>             $paramObj->img1,                            //图片
                  'img2'                =>             $paramObj->img2,                            //图片
                  'img3'                =>             $paramObj->img3,                            //图片
                  'introduce'           =>             $paramObj->introduce,                       //产品介绍
                  'is_show'             =>             $paramObj->is_show,                         //显示状态
                  'created'             =>             time()                                      //添加时间
        ];

        return $this->mShopProduct->insert($aData);
    }

    /**
     * 商品修改
     * @param $paramObj
     * @return mixed
     */
    public function ProductUp($paramObj)
    {

        $paramObj->img1 = !empty($paramObj->img1) ?
            $paramObj->img1 =  QiniuUpload::instance()->upload_files($paramObj->img1):$paramObj->img1_old;
        $paramObj->img2 = !empty($paramObj->img2) ?
            $paramObj->img2 =  QiniuUpload::instance()->upload_files($paramObj->img2):$paramObj->img2_old;
        $paramObj->img3 = !empty($paramObj->img3) ?
            $paramObj->img3 =  QiniuUpload::instance()->upload_files($paramObj->img3):$paramObj->img3_old;

        $aData = [
            'product_id'          =>             $paramObj->id,                              //产品id
            'product_name'        =>             $paramObj->product_name,                    //产品名称
            'price'               =>             $paramObj->price,                           //价格
            'brand'               =>             $paramObj->brand,                           //品牌
            'spec'                =>             $paramObj->spec,                            //规格
            'sort'                =>             $paramObj->sort,                            //分类
            'stock'               =>             $paramObj->stock,                           //库存
            'place'               =>             $paramObj->place,                           //产地
            'business_id'         =>             $paramObj->business_id,                     //商家id
            'img1'                =>             $paramObj->img1,                            //图片
            'img2'                =>             $paramObj->img2,                            //图片
            'img3'                =>             $paramObj->img3,                            //图片
            'introduce'           =>             $paramObj->introduce,                       //产品介绍
            'is_show'             =>             $paramObj->is_show,                         //显示状态
            'created'             =>             time()                                      //添加时间
        ];

        return $this->mShopProduct->update($aData,['product_id'=>$paramObj->id]);

    }
    /**
     * 获取商品信息
     * @param $id
     * @return mixed
     */
    public function GetProductId($id)
    {
        return $this->mShopProduct->GetProductId($id);
    }


    /**
     * 商家列表
     * @return mixed
     */
    public function BusinessList()
    {
        return $this->mShopBusiness->BusinessList();
    }

    /**
     * 商家添加
     * @param $paramObj
     * @return int
     */
    public function BusinessAdd($paramObj)
    {
        $paramObj->logo = !empty($paramObj->logo) ?
            $paramObj->logo      =    QiniuUpload::instance()->upload_files($paramObj->logo):'';

        $aData = [
            'business_id'         =>             $paramObj->id,                              //商家id
            'business_name'       =>             $paramObj->business_name,                   //商家名称
            'person'              =>             $paramObj->person,                          //联系人
            'phone'               =>             $paramObj->phone,                           //联系电话
            'area'                =>             $paramObj->area,                            //地区
            'logo'                =>             $paramObj->logo,                            //logo
            'introduce'           =>             $paramObj->introduce,                       //商家介绍
            'is_show'             =>             $paramObj->is_show,                         //显示状态
            'created'             =>             time()                                      //添加时间
        ];

        return $this->mShopBusiness->insert($aData);
    }

    /**
     * 商家修改
     * @param $paramObj
     * @return bool
     */
    public function BusinessUp($paramObj)
    {

        $paramObj->logo = !empty($paramObj->logo) ?
            $paramObj->logo      =    QiniuUpload::instance()->upload_files($paramObj->logo):$paramObj->logo_old;

        $aData = [
            'business_id'         =>             $paramObj->id,                              //商家id
            'business_name'       =>             $paramObj->business_name,                   //商家名称
            'person'              =>             $paramObj->person,                          //联系人
            'phone'               =>             $paramObj->phone,                           //联系电话
            'area'                =>             $paramObj->area,                            //地区
            'logo'                =>             $paramObj->logo,                            //logo
            'introduce'           =>             $paramObj->introduce,                       //商家介绍
            'is_show'             =>             $paramObj->is_show,                         //显示状态
            'created'             =>             time()                                      //添加时间
        ];

        return $this->mShopBusiness->update($aData,['business_id'=>$paramObj->id]);
    }

    /**
     * 获取商家信息
     * @param $id
     * @return mixed
     */
    public function GetBusinessId($id)
    {
        return $this->mShopBusiness->GetBusinessId($id);
    }




    /**
     *分类列表
     */
    public function CatesList()
    {
        return $this->mCategory->CatesList();
    }
    public function SelectCategory($id,$fids){

        return $this->mCategory->SelectCategory($id,$fids);
    }
    /**
     *父级列表
     */
    public function CatListfa()
    {
        return $this->mCategory->CatListfa();
    }
    /**
     *添加商品
     */
    public function AddCategory($paramObj)
    {
        $data=array(
            'cate_name'=>$paramObj->cate_name ,
            'fid'=>$paramObj->fid,
            'statue'=>1,
        );
        return  $this->mCategory->insert($data);

    }


    /**
     *修改商品
     */
    public function upCategory($paramObj)
    {
        $data=array(
            'cate_name'=>$paramObj->cate_name ,
            'fid'=>$paramObj->fid
        );
        return  $this->mCategory->update($data,['c_id'=>$paramObj->c_id]);

    }
    /**
     *是否存在id
     */
    public function CatcFirst($paramObj)
    {
        return $this->mCategory->CatFirst($paramObj->c_id );
    }
    /**
     *删除分类（修改状态）
     */
    public function DelectCategory($paramObj)
    {
        $data=array(
            'statue'=>2
        );
        return  $this->mCategory->update($data,['c_id'=>$paramObj->c_id]);
    }
    /**
     *批量删除
     */
    public function ALLDelectCategory($ids)
    {
        $data=array(
            'statue'=>2
        );
        return  $this->mCategory->update($data,['c_id'=>$ids]);
    }
    /**
     *查询是子级还是父级
     */
    public function FatherCategory($paramObj)
    {
        return $this->mCategory->FatherCategory($paramObj->c_id );
    }



    /**
     *查询是父级下面的子级
     */
    public function ParpentCategory($paramObj)
    {
        return $this->mCategory->ParpentCategory($paramObj->c_id );
    }

    /**
     *规格列表
     */
    public function Specificate()
    {
        return $this->mSpecificate->CatesList();
    }
    public function SelectSpecificate($id,$fids){

        return $this->mSpecificate->SelectCategory($id,$fids);
    }
    /**
     *规格父级列表
     */
    public function Specificatefa()
    {
        return $this->mSpecificate->CatListfa();
    }
    /**
     *规格是否存在id
     */
    public function SpecFirst($paramObj)
    {
        return $this->mSpecificate->CatFirst($paramObj->s_id );
    }
    /**
     *添加规格
     */
    public function AddSpecificate($paramObj)
    {
        $data=array(
            'spec_name'=>$paramObj->spec_name ,
            'fid'=>$paramObj->fid,
            'statue'=>1,
        );
        return  $this->mSpecificate->insert($data);

    }
    /**
     *修改规格
     */
    public function upSpecificate($paramObj)
    {
        $data=array(
            'spec_name'=>$paramObj->spec_name ,
            'fid'=>$paramObj->fid
        );
        return  $this->mSpecificate->update($data,['s_id'=>$paramObj->s_id]);

    }
    /**
     *删除分类（修改状态）
     */
    public function DelectSpecificate($paramObj)
    {
        $data=array(
            'statue'=>2
        );
        return  $this->mSpecificate->update($data,['s_id'=>$paramObj->s_id]);
    }

    /**
     * 品牌列表
     * @return mixed
     */
    public function BrandList()
    {
        return  $this->mShopBrand->BrandList();
    }

    /**
     * 品牌录入
     * @param $paramObj
     * @return int
     */
    public function BrandAdd($paramObj)
    {
        $aData = [
            'brand_id'            =>             $paramObj->id,                              //品牌id
            'brand_name'          =>             $paramObj->brand_name,                      //品牌名称
            'is_show'             =>             $paramObj->is_show,                        //是否显示
            'created'             =>             time()                                      //时间
        ];

        return $this->mShopBrand->insert($aData);
    }

    /**
     * 品牌编辑
     * @param $paramObj
     * @return bool
     */
    public function BrandUp($paramObj)
    {
        $aData = [
            'brand_id'            =>             $paramObj->id,                              //品牌id
            'brand_name'          =>             $paramObj->brand_name,                      //品牌名称
            'is_show'             =>             $paramObj->is_show,                         //是否显示
            'created'             =>             time()                                      //时间
        ];

        return $this->mShopBrand->update($aData,['brand_id'=>$paramObj->id]);

    }

    /**
     * 获取品牌
     * @param $id
     * @return mixed
     */
    public function GetBrandId($id)
    {
        return $this->mShopBrand->GetBrandId($id);
    }

    /**
     * 品牌删除
     * @param $aData
     * @return int
     */
    public function BrandDel($aData)
    {
        return $this->mShopBrand->delete(['brand_id'=>$aData->id]);
    }


}
?>